#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

queue * q_create(int size) 
{
	queue 	*q;
	int		i;

	q = (queue *)malloc(sizeof(queue));
	if (q == NULL) return NULL;
	q->size = size+1;
	q->data = (void **)malloc(sizeof(void *) * q->size);
	if (q->data == NULL) {
		free(q);
		return NULL;
	}
	for (i = 0; i < q->size; i++) {
		q->data[i] = NULL;
	}
	q->front = q->rear = 0;
	return q;
}
void q_push(queue *que, void *e) 
{
	if ((que->rear+1) % que->size == que->front) {
		printf("queue is full...\n");
		return;
	}
	que->data[que->rear] = e;
	que->rear = (que->rear+1) % que->size;
}

int q_size(queue *que)
{
	return (que->rear-que->front+que->size)%que->size;
}

void *q_pop(queue *que)
{
	void 	*p;

	if (q_empty(que)) return NULL;
	p = que->data[que->front];
	que->front = (que->front+1) % que->size;

	return p;
}

int q_empty(queue *que)
{
	return que->front == que->rear;
}

void q_destroy(queue *que) 
{
	free(que->data);
	free(que);
}
